package com.huayi.breakpoint_continuation_interface.utils;


import lombok.Data;

import java.security.MessageDigest;
import java.time.format.DateTimeFormatter;
import java.util.Date;

import static java.time.LocalTime.now;
import static java.time.LocalTime.ofNanoOfDay;


public class MD5Util {


    /***
     * MD5加密 生成32位md5码
     * @return 返回32位md5码
     */

    public static String md5Encode(String str) throws Exception {
        MessageDigest md5 = null;

        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (Exception e) {
            System.out.println(e.toString());
            e.printStackTrace();
            return "";
        }

        byte[] byteArray = str.getBytes("UTF-8");

        byte[] md5Bytes = md5.digest(byteArray);

        StringBuffer hexValue = new StringBuffer();

        for (int i = 0; i < md5Bytes.length; i++) {
            int val = ((int) md5Bytes[i]) & 0xff;
            if (val < 16) {
                hexValue.append("0");
            }
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString();
    }


    /**
     * 加密
     * @param str
     * @return
     */

    public static String convertMD5(String str) {
        char[] a = str.toCharArray();
        for (int i = 0; i < a.length; i++) {
            a[i] = (char) (a[i] ^ 't');
        }
        String s = new String(a);
        return s;

    }

    /**
     * 解密算法 (执行一次加密)进行两次解密
     */

    public static String md5Decode(String str) {
        return convertMD5(convertMD5(str));
    }

// 测试一下

    public static void main(String args[]) throws Exception {
        String s = new String("13917114404");
        String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiIxIiwiZXhwIjoxNjU2MzM2MzUwLCJpYXQiOjE2NTYzMzI3NTB9.aBN7E2z8kf-8JJstv1RAzsqCg8UmdMdkXlYPOIIJkEM";
        String key = now() + "huayi" + s;
        System.out.println("原始：" + key);
        System.out.println("MD5后：" + md5Encode(key));
        System.out.println("加密的：" + md5Encode(convertMD5(key)));
        System.out.println("解密的：" + md5Decode(key));
        System.out.println("解MD5的：" + convertMD5(key));
        System.out.println(token.equals(md5Decode(key)));


    }


}
